<?php

if (!defined('BASEPATH'))
    exit('No Direct Access Allowed !');

class admin_soal extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'form'));
    }

    function index() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $this->load->model('m_soal');
            $p = $this->uri->segment(2);
            $u = $this->uri->segment(3);
            if ($u == NULL) {
                redirect('admin_home');
            } else if ($p == NULL) {
                redirect('admin_home');
            }


            $page = $this->uri->segment(4);
            $limit = 10;
            if (!$page):
                $offset = 0;
            else:
                $offset = $page;
            endif;
            $d['tot'] = $offset;
            $tot_hal = $this->db->get_where('tb_soal', array('id_jenis' => $u));
            $config['base_url'] = site_url('admin_soal/index/' . $u . '/');
            $config['total_rows'] = $tot_hal->num_rows();
            $config['per_page'] = $limit;
            $config['uri_segment'] = 4;
            $this->pagination->initialize($config);
            $d["paginator"] = $this->pagination->create_links();
            //$d['data_get'] = $this->m_jenis->ambil($limit, $offset);
            $d['nama_soal'] = $this->m_soal->ambil_judul($u);
            $d['data_soal'] = $this->m_soal->ambil_soal($u, $limit, $offset);
            $d['id'] = $u;
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/isi', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function tambah() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
           
            $d['id_soal'] = "";
            $d['txt_soal'] = "";
            $d['jwb_a'] = "";
            $d['jwb_b'] = "";
            $d['jwb_c'] = "";
            $d['jwb_d'] = "";
            $d['val_1'] = "";
            $d['val_2'] = "";
            $d['val_3'] = "";
            $d['val_4'] = "";
            $d['param'] = "";
            $d['filegambar0'] = "default.gif";
            $d['filegambar1'] = "default.gif";
            $d['filegambar2'] = "default.gif";
            $d['filegambar3'] = "default.gif";
            $d['filegambar4'] = "default.gif";
            $id = $this->uri->segment(3);
            if ($id == NULL) {
                redirect('admin_home');
            }
            $d['st'] = "Tambah";
            $this->load->model('m_soal');
            $d['nama_soal'] = $this->m_soal->ambil_judul($id);
            $d['id'] = $id;
            $d['param'] = $id;
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/input_soal', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function edit() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $param = $this->uri->segment(3);
            $id = $this->uri->segment(4);
            $this->load->model('m_soal');
            $ds = $this->m_soal->edit_soal($id);
            $d['nama_soal'] = $this->m_soal->ambil_judul($param);
            if ($ds != NULL) {
                $d['txt_soal'] = $ds->text_soal;
            } else {
                redirect('admin_home');
            }

            //menampilkan jawaban per-id 
            $jawaban = $this->m_soal->edit_jawaban($id);

            $d['jwb_a'] = $jawaban[0]->text_jawaban;
            $d['jwb_b'] = $jawaban[1]->text_jawaban;
            $d['jwb_c'] = $jawaban[2]->text_jawaban;
            $d['jwb_d'] = $jawaban[3]->text_jawaban;
            //set checked 
            $d['val_1'] = $jawaban[0]->values;
            $d['val_2'] = $jawaban[1]->values;
            $d['val_3'] = $jawaban[2]->values;
            $d['val_4'] = $jawaban[3]->values;

            $d['filegambar0'] = $ds->filegambar;
            $d['filegambar1'] = $jawaban[0]->filegambar;
            $d['filegambar2'] = $jawaban[1]->filegambar;
            $d['filegambar3'] = $jawaban[2]->filegambar;
            $d['filegambar4'] = $jawaban[3]->filegambar;
            if ($ds->filegambar == NULL) {
                $d['filegambar0'] = "default.gif";
            }

            if ($jawaban[0]->filegambar == NULL) {
                $d['filegambar1'] = "default.gif";
            }
            if ($jawaban[1]->filegambar == NULL) {
                $d['filegambar2'] = "default.gif";
            }
            if ($jawaban[2]->filegambar == NULL) {
                $d['filegambar3'] = "default.gif";
            }
            if ($jawaban[3]->filegambar == NULL) {
                $d['filegambar4'] = "default.gif";
            }
            $d['st'] = "Edit";
            $d['id'] = $id;
            $d['param'] = $param;
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/input_soal', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function simpan() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            //$this->form_validation->set_rules('txt_soal', 'Soal', 'trim|required');
            //$this->form_validation->set_rules('jwb_a', 'Jawaban A', 'trim|required');
            //$this->form_validation->set_rules('jwb_b', 'Jawaban B', 'trim|required');
            //$this->form_validation->set_rules('jwb_c', 'Jawaban C', 'trim|required');
            //$this->form_validation->set_rules('jwb_d', 'Jawaban D', 'trim|required');
            if ($this->input->post('submit')) {
                
                /* if ($this->form_validation->run() == FALSE) {
                  $this->load->model('m_soal');
                  $d['id'] = $this->input->post('id');
                  $d['st'] = $this->input->post('st');
                  $d['nama_soal'] = $this->m_soal->ambil_judul($d['id']);
                  $d['txt_soal'] = $this->input->post('txt_soal');
                  $d['jwb_a'] = $this->input->post('jwb_a');
                  $d['jwb_b'] = $this->input->post('jwb_b');
                  $d['jwb_c'] = $this->input->post('jwb_c');
                  $d['jwb_d'] = $this->input->post('jwb_d');
                  $d['param'] = $this->input->post('param');
                  $d['val_1'] = $d['val_2'] = $d['val_3'] = $d['val_4'] = "";
                  $this->load->view('admin/global/header');
                  $this->load->view('admin/soal/input_soal', $d);
                  $this->load->view('admin/global/footer');
                  } else { */
                $st = $this->input->post('st');
                if ($st == "Tambah") {
                    $this->do_upload();
                    
                    //redirect('admin_soal/index/' . $param);
                } else if ($st == "Edit") {
                    $soal = $this->input->post('txt_soal');
                    $id = $this->input->post('id');
                    $this->load->model('m_soal');

                    $a = $this->input->post('jwb_a');
                    $b = $this->input->post('jwb_b');
                    $c = $this->input->post('jwb_c');
                    $d = $this->input->post('jwb_d');
                    $post_a = $post_b = $post_c = $post_d = '0';
                    if (isset($_POST['chk_a'])) {
                        $post_a = '1';
                    }
                    if (isset($_POST['chk_b'])) {
                        $post_b = '1';
                    }
                    if (isset($_POST['chk_c'])) {
                        $post_c = '1';
                    }
                    if (isset($_POST['chk_d'])) {
                        $post_d = '1';
                    }
                    //mengambil id_jawaban
                    $jawaban = $this->m_soal->edit_jawaban($id);
                    $id_jwb_a = $jawaban[0]->id_jawaban;
                    $id_jwb_b = $jawaban[1]->id_jawaban;
                    $id_jwb_c = $jawaban[2]->id_jawaban;
                    $id_jwb_d = $jawaban[3]->id_jawaban;

                    //update
                    $this->m_soal->update_soal($id, $soal);
                    $this->m_soal->update_jawaban($id_jwb_a, $a, $post_a);
                    $this->m_soal->update_jawaban($id_jwb_b, $b, $post_b);
                    $this->m_soal->update_jawaban($id_jwb_c, $c, $post_c);
                    $this->m_soal->update_jawaban($id_jwb_d, $d, $post_d);
                    //upload image
                    $this->do_upload();               
                    
                    
                }
                //}
            }
        }
    }

    function hapus() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            $u = $this->uri->segment(4);
            $r = $this->uri->segment(3);
            $this->load->model('m_soal');
            $this->m_soal->hapus($u);
            redirect('admin_soal/index/' . $r);
        }
    }

    function cari() {
        $cek = $this->session->userdata('logged_in');
        if (empty($cek)) {
            header('location:' . site_url('admin'));
        } else {
            if ($this->input->post('cari') == "") {
                $kata = "";
            } else {
                $kata = $this->input->post('cari');
            }
            $this->load->model('m_soal');
            $u = $this->input->post('id');
            $page = $this->uri->segment(4);
            $limit = 10;
            if (!$page):
                $offset = 0;
            else:
                $offset = $page;
            endif;
            $d['tot'] = $offset;
            $tot_hal = $this->m_soal->cari_like($kata, $u);
            $config['base_url'] = site_url('admin_home/index/' . $u);
            $config['total_rows'] = $tot_hal->num_rows();
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $d["paginator"] = $this->pagination->create_links();
            //$d['data_get'] = $this->m_jenis->ambil($limit, $offset);
            $d['nama_soal'] = $this->m_soal->ambil_judul($u);
            $d['data_soal'] = $this->m_soal->cari($kata, $u, $limit, $offset);
            $d['id'] = $u;

            //print_r($config['total_rows']);
            $this->load->view('admin/global/header');
            $this->load->view('admin/soal/isi', $d);
            $this->load->view('admin/global/footer');
        }
    }

    function do_upload() {
        $this->load->model('m_soal');
        //post
        $st = $this->input->post('st');
        $txt = $this->input->post('txt_soal');
        $id = $this->input->post('id');
        $param = $this->input->post('param');
        


        //Upload Start
        $config['upload_path'] = "./images/real/";
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '2000';
        $config['max_width'] = '800';
        $config['max_height'] = '600';

        $this->load->library('upload', $config);
        for ($i = 0; $i < 6; $i++) {
            $data[$i] = array('file_name' => NULL); //default data
            if ($this->upload->do_upload('image' . $i)) {
                $data[$i] = $this->upload->data();
                /* PATH */
                $source = "./images/real/" . $data[$i]['file_name'];
                $destination_thumb = "./images/small/";
                $destination_medium = "./images/medium/";

                // Permission Configuration
                // chmod($source, 0777);

                /* Resizing Processing */
                // Configuration Of Image Manipulation :: Static
                $this->load->library('image_lib');
                $img['image_library'] = 'GD2';
                $img['create_thumb'] = TRUE;
                $img['maintain_ratio'] = TRUE;

                /// Limit Width Resize
                $limit_medium = 100;
                $limit_thumb = 50;

                // Size Image Limit was using (LIMIT TOP)
                $limit_use = $data[$i]['image_width'] > $data[$i]['image_height'] ? $data[$i]['image_width'] : $data[$i]['image_height'];

                // Percentase Resize
                if ($limit_use > $limit_medium || $limit_use > $limit_thumb) {
                    $percent_medium = $limit_medium / $limit_use;
                    $percent_thumb = $limit_thumb / $limit_use;
                }

                //// Making THUMBNAIL ///////
                $img['width'] = $limit_use > $limit_thumb ? $data[$i]['image_width'] * $percent_thumb : $data[$i]['image_width'];
                $img['height'] = $limit_use > $limit_thumb ? $data[$i]['image_height'] * $percent_thumb : $data[$i]['image_height'];

                // Configuration Of Image Manipulation :: Dynamic
                $img['thumb_marker'] = '';
                $img['quality'] = '100%';
                $img['source_image'] = $source;
                $img['new_image'] = $destination_thumb;

                // Do Resizing
                $this->image_lib->initialize($img);
                $this->image_lib->resize();
                $this->image_lib->clear();

                ////// Making MEDIUM /////////////
                $img['width'] = $limit_use > $limit_medium ? $data[$i]['image_width'] * $percent_medium : $data[$i]['image_width'];
                $img['height'] = $limit_use > $limit_medium ? $data[$i]['image_height'] * $percent_medium : $data[$i]['image_height'];

                // Configuration Of Image Manipulation :: Dynamic
                $img['thumb_marker'] = '';
                $img['quality'] = '100%';
                $img['source_image'] = $source;
                $img['new_image'] = $destination_medium;

                // Do Resizing
                $this->image_lib->initialize($img);
                $this->image_lib->resize();
                $this->image_lib->clear();
            }
        }
        // Initialize Array 
        $arr = array(0 => $data[1], 1 => $data[2], 2 => $data[3], 3 => $data[4], 4 => $data[5]);
//      echo '<pre>';
//      print_r($arr);
//       echo '</pre>';
        
        
        if ($st == "Edit") {
            $temp = $this->m_soal->get_id_soal($txt);
            $id_soal = $temp->id_soal;
            $jawaban = $this->m_soal->edit_jawaban($id_soal);
            $id_jwb_a = $jawaban[0]->id_jawaban;
            $id_jwb_b = $jawaban[1]->id_jawaban;
            $id_jwb_c = $jawaban[2]->id_jawaban;
            $id_jwb_d = $jawaban[3]->id_jawaban;
            $this->m_soal->update_image_soal($id_soal, $arr[0]['file_name']);
            $this->m_soal->update_image_jawaban($id_jwb_a, $arr[1]['file_name']);
            $this->m_soal->update_image_jawaban($id_jwb_b, $arr[2]['file_name']);
            $this->m_soal->update_image_jawaban($id_jwb_c, $arr[3]['file_name']);
            $this->m_soal->update_image_jawaban($id_jwb_d, $arr[4]['file_name']);
        }

        if ($st == "Tambah") {
            $txt = $this->input->post('txt_soal');
            $this->load->model('m_soal');
            $this->m_soal->tambah_soal($txt, $id, $arr[0]['file_name']);
            //print_r($a);
            $data = $this->m_soal->get_id_soal($txt, $arr[0]['file_name']);
            $id_soal = $data->id_soal;
            $a = $this->input->post('jwb_a');
            $b = $this->input->post('jwb_b');
            $c = $this->input->post('jwb_c');
            $d = $this->input->post('jwb_d');

            //setting value checkbox
            $post_a = $post_b = $post_c = $post_d = '0';
            if (isset($_POST['chk_a'])) {
                $post_a = '1';
            }
            if (isset($_POST['chk_b'])) {
                $post_b = '1';
            }
            if (isset($_POST['chk_c'])) {
                $post_c = '1';
            }
            if (isset($_POST['chk_d'])) {
                $post_d = '1';
            }
            $this->m_soal->tambah_jawaban($a, $post_a, $id_soal, $arr[1]['file_name']);
            $this->m_soal->tambah_jawaban($b, $post_b, $id_soal, $arr[2]['file_name']);
            $this->m_soal->tambah_jawaban($c, $post_c, $id_soal, $arr[3]['file_name']);
            $this->m_soal->tambah_jawaban($d, $post_d, $id_soal, $arr[4]['file_name']);
        }
        redirect('admin_soal/index/' . $param);
    }

}